﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Mobile
{
    public partial class search : PageBase
    {
        //必须改造，使用viewstate保存
        private static PagedDataSource pdds = new PagedDataSource();
        private Hashtable user = new Hashtable();
        protected void Page_Load(object sender, EventArgs e)
        {
            SetDivePage(ref pdds, ref Page1, 2, ref Rept1);
            user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));

            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["phonebookid"]))
                {
                    btn_query_name_Click(btn_query_name, new EventArgs());
                }
            }
        }

        public override void QueryData()
        {
            string filter = " 1 = 1 ";
            if (!string.IsNullOrEmpty(txt_name.Value))
                filter += string.Format(" and employeename like '%{0}%' or mobile like '%{0}%' or dbo.fun_getPY(employeename) like '%{0}%' ", txt_name.Value);

            if (rbl_range.SelectedValue == "本集团")
                filter += string.Format(" and groupid = {0} ", user["groupid"]);
            else if (rbl_range.SelectedValue == "共享集团")
                filter += string.Format(" and groupid <> {0} ", user["groupid"]);

            if (!string.IsNullOrEmpty(Request["phonebookid"]))
            {
                filter += string.Format(" and phonebookid = {0} ", Request["phonebookid"]);
            }

            DataSet ds = getphonebook();
            Commons.DataUtil.DataFilter(ds, filter, "");


            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataView dv = ds.Tables[0].DefaultView;
                if (dv.Count == 0)
                {
                    Page1.Visible = false;
                    Rept1.Visible = false;
                }
                else
                {
                    Rept1.Visible = true;
                    Page1.Visible = true;
                    pdds.DataSource = dv;
                }
            }
            else
            {
                Rept1.Visible = false;
                Page1.Visible = false;
                pdds.DataSource = null;
            }
            GVDataBind(ref Rept1, ref Page1, ref pdds);
        }

        protected void btn_query_name_Click(object sender, EventArgs e)
        {
            div_phonebook.Visible = true;
            QueryData();
        }

        public DataSet getphonebook()
        {
            string cachename = "phonebook_jklsjdkllk892sdjkl_" + Commons.StrUtil.FormatValue(Session["userid"]);
            DataSet result = Cache.Get(cachename) as DataSet;

            if (result == null)
            {
                result = PhoneBook.Group.PhoneBook.queryphonebook(Commons.StrUtil.FormatValue(user["employeeid"]));
                if (Commons.DataUtil.IsValidate(result))
                    Cache.Add(cachename, result, null, System.Web.Caching.Cache.NoAbsoluteExpiration,
                        new TimeSpan(3000), System.Web.Caching.CacheItemPriority.Default, null);
            }
            return result;
        }
    }
}